package DataStructure;

import java.util.Scanner;

/**
 *
 * 数组模拟栈
 * @time: 2021/3/28/028 21:32
 * @author: Zhurunhe
 */
public class IntStackTest {
    public static void main(String[] args) {
        IntStack intStack=new IntStack(5);
        Scanner scanner = new Scanner(System.in);
        String key = "";

        boolean loop = true;
        while (loop) {
            System.out.println("push:添加数据到队列中");
            System.out.println("pop:获取队列中的头数据");
            System.out.println("show:显示队列中的数据");
            System.out.println("e:退出");
            key = scanner.next();
            switch (key) {
                case "show":
                    try {
                        intStack.show();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case "pop":
                    try {
                        System.out.println(intStack.pop());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "push":
                    System.out.println("请输入一个数值");
                    intStack.push(scanner.nextInt());
                    break;

                case "e":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");

    }
}
class IntStack{
    private int[] intstack;
    private int maxsize;
    private int top=-1;

    public IntStack(int maxsize){
        this.maxsize=maxsize;
        intstack=new int[maxsize];
    }
    public boolean isFull(){
        return top==maxsize-1;
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public void push(int values){
        if (isFull()){
            System.out.println("---------栈已满");
            return;
        }
        top++;
        intstack[top]=values;
    }
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("---------栈空");
        }
        int valuse=intstack[top];
        top--;
        return valuse;
    }

    public void show(){
        if (isEmpty()){
            throw new RuntimeException("---------栈空");
        }
        for (int i = top; i >=0 ; i--) {
            System.out.println(intstack[i]);


        }
    }
}
